from app.outdoor.models import Activity
from common.dictInfo import model_to_dict
from common.authCheck.userAuth import check_login
from common.paginator import paginator
from django.http import JsonResponse
from rest_framework.views import APIView


class ActivityListInfoView(APIView):
    INCLUDE_FIELDS = [
        'name', 'status', 'activity_type', 'sign_up_start_time', 'group_name'
    ]

    @check_login
    def get(self, request):
        """
        获取户外活动列表
        :param request:
        :return:
        """
        try:
            activity = Activity.objects.filter(status__in=['2', '3', '4'])
            pageActivity = paginator(activity, request.GET.get('page'))
            activityDict = [model_to_dict(ac, fields=self.INCLUDE_FIELDS) for ac in pageActivity]
            return JsonResponse({
                'status': True,
                'activityList': activityDict,
                'has_previous': pageActivity.has_previous(),
                'has_next': pageActivity.has_next()
            })
        except Exception as ex:
            return JsonResponse({
                'status': False,
                'errMsg': str(ex)
            })
